//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: baseJs.imgUrl,
    imgList: [],
    imgStr: "",
    // 
    uid:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.GetUserInfoById();
  },

  //GetUserInfoById
  GetUserInfoById: function() {
    var that = this;
    var uid = this.data.uid;
    var imgList = this.data.imgList;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=UserRealName&UID=" + uid),
      success: function(res) {
        console.log(JSON.parse(baseJs.decryUrl(res.data)));
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData.BusinesslicensePic.split(';');
        for (var i = 0; i < json.length; i++) {
          if (json[i].length > 0) {
            imgList.push(json[i]);
          }
        }
        that.setData({
          imgList: imgList,
        })
        console.log(imgList);
      }
    })
  },
  // delTap
  delTap: function(e) {
    var imgList = this.data.imgList;
    imgList.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: imgList,
    })
  },
  // chooseImg
  chooseImg: function(e) {
    var that = this;
    var base64 = this.data.base64;
    var imgSrc = this.data.imgSrc;
    var imgUrl = this.data.imgUrl;
    var imgList = this.data.imgList;
    wx.chooseImage({
      // 最多可以选择的图片张数，默认9
      count: 1,
      // 可以指定是原图还是压缩图，默认二者都有
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        wx.request({
          url: tempFilePaths,
          method: 'GET',
          responseType: 'arraybuffer',
          success: function(res) {
            base64 = wx.arrayBufferToBase64(res.data);
            that.setData({
              base64: base64,
              imgSrc: 'data:image/jpg;base64,' + base64
            })
            // console.log(base64)
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=LoadPic"),
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: 'post',
              data: baseJs.json2Form({
                HeadImage: base64,
              }),
              success: function(res) {
                console.log(res);
                imgList.push(res.data.ResultData);
                that.setData({
                  imgList: imgList,
                })
              }
            })
          }
        })
      }
    })
  },
  // upload
  upload: function() {
    var that = this;
    var uid = this.data.uid;
    var imgList = this.data.imgList;
    var imgStr = this.data.imgStr;
    imgStr = imgList.join(';');
    console.log(imgStr);
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=UserUpdateRealName&UID=" + uid + "&IdCardFontPic=&IdCardBackPic=&Businesslicense=" + imgStr + "&ShopStorePic="),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data));
        if (json.ResultFlag == 1) {
          wx.navigateTo({
            url: '/packageA/uploadBusinessImg/uploadBusinessImg'
          })
        } else {
          wx.showToast({
            title: json.ResultMsg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },
  // 
})